package com.google.code.netbuilder.dal.providersimpl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.google.code.netbuilder.dal.entities.Cable;
import com.google.code.netbuilder.dal.entities.CableWire;
import com.google.code.netbuilder.dal.providers.ICableWireProvider;
import com.google.code.netbuilder.dal.services.JDBCServise;

public class CabelWireProviderJDBC implements ICableWireProvider {

	@Override
	public void save(CableWire entity) {
		String query = "INSERT INTO cable_wire(id_cable, id_wire, date)" +
				" VALUES (?,?,?);";
		Connection con = null;
		PreparedStatement pst = null;
		try
		{
			con = JDBCServise.getConnection();
			if (con == null) throw new SQLException("No Data Base Connection");
			int rs=-1;
			pst = con.prepareStatement(query);
			pst.setInt(1, entity.getCableId());
			pst.setInt(2, entity.getWireId());
			pst.setDate(3, new Date(entity.getDate().getTime()));
			pst.executeUpdate();
		}
		catch(SQLException e)
		{
			throw new RuntimeException(e);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
				}
		}
	}

	@Override
	public void update(CableWire entity) {
		String query = "UPDATE Cable_wire SET id_cable=?  WHERE id_wire=?;";
		
		Connection con = null;
		PreparedStatement pst = null;
		try {
			con = JDBCServise.getConnection();
			if (con == null) throw new SQLException("No Data Base Connection");
			pst = con.prepareStatement(query);
			pst.setInt(1, entity.getCableId());
			pst.setInt(2, entity.getWireId());
			pst.executeUpdate();		
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
				}
		}

	}

	@Override
	public void delete(CableWire entity) {
		String query = "DELETE FROM cable_wire WHERE id_wire=? AND id_cable=?;";
		
		Connection con = null;
		PreparedStatement pst = null;
		try {
			con = JDBCServise.getConnection();
			if (con == null) throw new SQLException("No Data Base Connection");
			pst = con.prepareStatement(query);
			pst.setInt(1, entity.getWireId());	
			pst.setInt(2, entity.getCableId());
			pst.executeUpdate();	
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}		
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
				}
		}
	}

	@Override
	public List findAll() {
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<CableWire> cableWire = null;
		
		try {
			con = JDBCServise.getConnection();
        
			pst = con.prepareStatement("Select * From cable_wire");
			rs = pst.executeQuery();
        
			cableWire = new ArrayList<CableWire>();
        
			while (rs.next()) {
				CableWire cw = new CableWire();
				cw.setCableId(rs.getInt("id_cable"));
				cw.setWireId(rs.getInt("id_wire"));
				cw.setDate(rs.getDate("date").getTime());
				cableWire.add(cw);
			}
		} catch (SQLException exc)
		{
			throw new RuntimeException(exc);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
				if (rs != null) rs.close();
			} catch (SQLException exc) {
				throw new RuntimeException(exc);
				}
		}	
		
        return cableWire;
	}

	@Override
	public CableWire findByID(Integer id) {
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		CableWire cw = null;
		
		try {
			con = JDBCServise.getConnection();
        
			pst = con.prepareStatement("Select * From cable_wire where id_wire="+id.toString());
			rs = pst.executeQuery();
        
			if(!rs.first()) throw new SQLException("Cable not found");;
			
			
			cw = new CableWire();
			cw.setCableId(rs.getInt("id_cable"));
			cw.setWireId(rs.getInt("id_wire"));
			cw.setDate(rs.getDate("date").getTime());
			
			
		} catch (SQLException exc)
		{
			throw new RuntimeException(exc);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
				if (rs != null) rs.close();
			} catch (SQLException exc) {
				throw new RuntimeException(exc);
				}
		}	
		
        return cw;
	}

}
